<?php

class WoguProductsAction extends CommonAction
{
    public function man() {
        $cate_id = intval($_GET['cate_id']);
        $where = 'flag=0';
        if($cate_id) {
            $where .= ' AND cate_id='.$cate_id;
        }
		$model = M();
		$sql = 'SELECT COUNT(*) AS tcount FROM '.C("DB_PREFIX").'wogu_product WHERE '.$where;
		$count = $model->query($sql);
		$count = $count[0]['tcount'];

		$sql = 'SELECT * FROM '.C("DB_PREFIX").'wogu_product WHERE '.$where;
		$this->_sqlList($model,$sql,$count,array(),'id');
		
		$catids = array();
		foreach($this->list as $v) {
		    $catids[] = $v['cate_id'];
		}
		if(!empty($catids)) {
		    $catids = array_unique($catids);
            $tmp = D('GoodsCategory')->query('SELECT * FROM '.C("DB_PREFIX").'goods_category WHERE cate_id IN('.implode(',', $catids).')');
            $cats = array();
            foreach($tmp as $v) {
                $cats[$v['cate_id']] = $v;
            }
            $this->assign('cats', $cats);
		}
        $this->display();
    }
    
    public function del() {
        D('WoguProduct')->query('UPDATE '.C("DB_PREFIX").'wogu_product SET flag=1 WHERE id='.intval($_GET['id']));
		$this->success (L('REMOVE_SUCCESS'), $_SERVER['HTTP_REFERER']);
    }
    
    public function add() {
        if($_POST['doadd']) {
            $title = addslashes($_POST['title']);
            $cate_id = intval($_POST['parent_id']);
            $is_qjd = intval($_POST['is_qjd']);
            $price = floatval($_POST['price']);
            $img = addslashes($_POST['img']);
            $source = addslashes($_POST['source']);
            $redirecturl = addslashes($_POST['redirecturl']);
            $content = addslashes($_POST['content']);
            $orderby = intval($_POST['orderby']);
            $width = $_POST['width'];
            $height = $_POST['height'];
            $dj = intval($_POST['dj']);
            D('WoguProduct')->query("INSERT ".C("DB_PREFIX")
            ."wogu_product (title,cate_id,price,img,source,redirecturl,content,width,height,is_qjd,orderby,dj)
            VALUES('{$title}','{$cate_id}','{$price}','{$img}','{$source}','{$redirecturl}','{$content}','{$width}','{$height}',{$is_qjd},{$orderby},{$dj})");
            exit('ok');
        }
        
        if($_GET['op'] == 'getimgsize') {
            $_GET['img'] = empty($_GET['host']) ? $_GET['img'] : 'http://'.$_GET['host'].$_GET['img'];
            $size = getimagesize($_GET['img']);
            if(!$size) exit('0');
            echo $size[0] . '|' . $size[1];
        } else {
    		$cate_list = D("GoodsCategory")->where('status = 1')->field('cate_id,parent_id,cate_name')->order('sort ASC,cate_id ASC')->select();
    		$cate_list = D("GoodsCategory")->toFormatTree($cate_list,'cate_name','cate_id','parent_id');
    		$this->assign("cate_list",$cate_list);
    		
    		$this->display();
    	}
    }
}
